﻿// -----------------------------------------------------------------------
// <copyright file="IsValidImageFormatAttribute.cs" company="EPAM Systems">
////Copyright (c) EPAM Systems, 2012. All rights reserved.
// </copyright>
// -----------------------------------------------------------------------

namespace MBOP.Website.CustomAttributes
{
	#region Usings

	using System.ComponentModel.DataAnnotations;
	using MBOP.Configuration.ConfigElements;
	using MBOP.Configuration.ConfigExtensions;

	#endregion

	/// <summary>
	/// Represents the image format validation attribute.
	/// </summary>
	public class IsValidImageFormatAttribute : ValidationAttribute
	{
		/// <summary>
		/// Checks the current value is correct or not.
		/// </summary>
		/// <param name="value">The current value</param>
		/// <returns>THe result of the validation process</returns>
		public override bool IsValid(object value)
		{
			string imageFormat = value as string;

			bool isValid = false;

			if (value != null)
			{
				if (imageFormat != null)
				{
					ImageFormatElementCollection imageFormatCollection = AppConfigurationSection.GetInstance().ImageFormats;

					isValid = imageFormatCollection.IsConditionComplete(index => imageFormatCollection[index].MimeType == imageFormat);
				}
			}
			else
			{
				isValid = true;
			}

			return isValid;
		}

		/// <summary>
		/// Applies formatting to an error message based on the data field where the error occurred.
		/// </summary>
		/// <param name="name">The data field name.</param>
		/// <returns>String which represents the formatted message</returns>
		public override string FormatErrorMessage(string name)
		{
			return string.Format(this.ErrorMessage, AppConfigurationSection.GetInstance().ImageFormats.ToString());
		}
	}
}